// 查询

// swagger 文档
defineRouteMeta({
  openAPI: {
    description: '用户列表查询',
    summary: '用户列表查询',
    tags: ['用户'],

    responses: {
      200: {
        description: '用户查询成功',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 200
                },
                message: {
                  type: 'string',
                  description: '状态信息'
                },
                data: {
                  type: 'array',
                  description: '用户信息',
                  items: {
                    type: 'object',
                    properties: {
                      id: {
                        type: 'number',
                        description: '用户id'
                      },
                      name: {
                        type: 'string',
                        description: '用户名称'
                      },
                      email: {
                        type: 'string',
                        description: '用户邮箱'
                      },
                      password: {
                        type: 'string',
                        description: '用户密码'
                      },
                      avatar: {
                        type: 'string',
                        description: '用户头像'
                      },
                      createdAt: {
                        type: 'string',
                        description: '用户创建时间'
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },

      500: {
        description: '用户查询失败',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 500
                },
                message: {
                  type: 'string',
                  description: '错误信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      }
    }
  }
});

import { desc } from 'drizzle-orm';
export default eventHandler(async event => {
  const { result } = new useResult(event);
  try {
    const users = await useDrizzle().select().from(tables.users).orderBy(desc(tables.users.id));
    return result(users);
  } catch (e: any) {
    return result(null, e.message, 500);
  }
});
